﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Component;
using DevExpress.Xpo;
using System.IO;

public partial class BackEnd_ProductImgList : System.Web.UI.UserControl
{
    protected string _ImgSmall = "";
    protected string _ImgBig = "";
    public string nameImage = "";
    public string _NameProduct = "";
    public Product _Product = null;
    public string _Url = BackEndQuery._AdminQuery + ConstString.NewL;
    FileInfo[] alFile = null;
    int _idPro = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        this._ImgSmall = @"OpenWindowUploadFile(""FileManagement/File.aspx?ImgSmall=1&ImgBig=1&id=dss&idimg=sds"",""window"",""width = 630px, height =520px"");";
        this._ImgBig = @"OpenWindowUploadFile(""FileManagement/File.aspx?ImgSmall=0&ImgBig=1&id=dss&idimg=sds"",""window"",""width = 630px, height =520px"");";
        _idPro = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._idPro));
        _Product = ProductDB.GetObjectByid(_idPro, true);
        DataBind(ProductImgDB.GetAllObjByIdProduct(_idPro));

        grid.RowCancelingEdit += delegate { Response.Redirect(Request.Url.ToString()); };
    }
    /// <summary>
    ///   public void ReGetAllImge(object sender, System.EventArgs e)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void ReGetAllImge(object sender, System.EventArgs e)
    {
        GetAllSmallImg(null);
        DataBind(ProductImgDB.GetAllObjByIdProduct(_idPro));
    }
    /// <summary>
    ///    public void GetAllImg()
    /// </summary>
    public void GetAllBigImg(ProductImg productImg)
    {
        if (productImg == null)
            return;
        string path = Server.MapPath(ConstString._PathFileProductBigImg);
        string path2 = "FileUpload/Product/";
        alFile = ProductImgDB.GetAllFileImg(path);
        if (alFile == null || alFile.Length <= 0)
            return;
        int order = 0;
        foreach (FileInfo file in alFile)
        {
            FileInfo fi = new FileInfo(ConvertType.ToString(HttpRuntime.AppDomainAppPath + ConvertType.ToString(productImg.PathImgBig).Replace("../", "").Replace("/", "\\")));
            if (fi.Exists == true)
                fi.Delete();
            string newNameImg = file.Name.Replace(file.Name.Split('.')[0], productImg.Id.ToString ());
            //string path1 = path2 + file.Name;
            string path4 = path2 + _Product.Id + "/Big/" + newNameImg;
            //if (ProductImgDB.TestExistFile(path4, ColumProductImg._PathImgBig) == false)
            //{
            productImg.PathImgBig = path4;
            productImg.Priority = order;
            productImg.Product = _Product;
            order++;
            string path3 = Server.MapPath(path2.Replace("/", "\\")) + _Product.Id + "\\Big\\";
            if (!Directory.Exists(path3))
                Directory.CreateDirectory(path3);

            file.CopyTo(path3 + "\\" + newNameImg, true);
            ProductImgDB.Save(productImg);
            //}
            file.Delete();
        }
    }
    /// <summary>
    ///    public void GetAllSmallImg()
    /// </summary>
    public void GetAllSmallImg(ProductImg productImg)
    {
        string path = Server.MapPath(ConstString._PathFileProductSmallImg);
        string path2 = "FileUpload/Product/";
        alFile = ProductImgDB.GetAllFileImg(path);
        ProductImg info = productImg;
        if (alFile != null && alFile.Length > 0)
        {
            int order = 0;
            foreach (FileInfo file in alFile)
            {
                
                //string path1 = path2 + file.Name;
                string path4 = path2 + _Product.Id + "/Small/";// file.Name
                //if (ProductImgDB.TestExistFile(path4, ColumProductImg._PathImgBig) == false)
                //{
                if (info == null)
                    info = new ProductImg(KetNoiCSDL.session);
                else
                {
                    FileInfo fi = new FileInfo(ConvertType.ToString(HttpRuntime.AppDomainAppPath + ConvertType.ToString(productImg.PathImgSmall).Replace("../", "").Replace("/", "\\")));
                    if (fi.Exists == true)
                        fi.Delete();
                }
                //info.PathImgSmall = path4;
                info.Priority = order;
                info.Product = _Product;
                order++;
                string path3 = Server.MapPath(path2.Replace("/", "\\")) + _Product.Id + "\\Small\\";
                if (!Directory.Exists(path3))
                    Directory.CreateDirectory(path3);
                ProductImgDB.Save(info);
                string newNameImg = file.Name.Replace(file.Name.Split('.')[0], info.Id.ToString());
                info.PathImgSmall = path4 + newNameImg;
                info.Save();
                file.CopyTo(path3 + "\\" + newNameImg, true);//file.Name

                //}
                file.Delete();
            }
        }
        GetAllBigImg(info);
    }

    /// <summary>
    ///     void DataBind(object al)
    /// </summary>
    /// <param name="al"></param>
    void DataBind(object al)
    {
        if (al != null)
        {
            grid.DataKeyNames = new string[] { ColumProductImg._Id };
            grid.DataSource = al;
            grid.DataBind();
        }
    }

    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        ProductImg productImg = new ProductImg(KetNoiCSDL.session);
        TextBox txtPriority = (TextBox)grid.Rows[e.RowIndex].FindControl("txtPriority");
        productImg = ProductImgDB.GetObjectByid(ConvertType.ToInt(id), true);
        productImg.Priority = ConvertType.ToInt(txtPriority.Text.Trim());
        GetAllSmallImg(productImg);
        if (ProductImgDB.Save(productImg) == 1)
        {
            this.lbMessage.Text = ConstString.capNhatTC;
        }

        Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            ProductImg intro = ProductImgDB.GetObjectByid(ConvertType.ToInt(id), true);
            if (ProductImgDB.Delete(intro) == 1)
            {
              //  string path1 = HttpRuntime.AppDomainAppPath + intro.PathImgBig.Replace("../", "").Replace("/", "\\");
                string path1 = Server.MapPath(intro.PathImgSmall.Replace("/","\\"));
                FileInfo fi = new FileInfo(path1);
                if (fi.Exists == true)
                    fi.Delete();
                path1 = Server.MapPath(intro.PathImgBig.Replace("/", "\\")); ;// HttpRuntime.AppDomainAppPath + intro.PathImgSmall.Replace("../", "").Replace("/", "\\");
                fi = new FileInfo(path1);
                if (fi.Exists == true)
                    fi.Delete();
                this.lbMessage.Text = ConstString.xoaTC;
            }
        }
        catch { this.lbMessage.Text = ConstString.xoaTB; }
        Response.Redirect(Request.Url.ToString());
    }

    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.btnRefresh.Enabled = false;
        string path = Server.MapPath(ConstString._PathFileProductBigImg);
        DirectoryInfo info = new DirectoryInfo(path);
        if (info.Exists)
        {
            foreach (FileInfo file in info.GetFiles())
            {
                file.Delete();
            }
        }
        else
        {
            info.Create();
        }
        path = Server.MapPath(ConstString._PathFileProductSmallImg);
        info = new DirectoryInfo(path);
        if (info.Exists)
        {
            foreach (FileInfo file in info.GetFiles())
            {
                file.Delete();
            }
        }
        else
        {
            info.Create();
        }
        grid.EditIndex = e.NewEditIndex;
        DataBind(Component.ProductImgDB.GetAllObjByIdProduct(_idPro));
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(Component.ProductImgDB.GetAllObjByIdProduct(_idPro));
    }

}
